//当前项目的路由模块

//1.导入Vue和VueRouter的包
import Vue from "vue"
import VueRouter from "vue-router"
import pathArr from "@/router/pathArr.js";
//导入需要的组件
import Login from "@/components/MyLogin";
import Register from "@/components/MyRegister";
import Game from "@/components/Game"
import Nothing from "@/components/Nothing"

//背景组件

//2.调用Vue.use()函数，把VueRouter安装为Vue插件
Vue.use(VueRouter)

//3.创建路由的实例对象
const router = new VueRouter({
    routes: [{
            path: '/',
            redirect: '/login'
        },
        //登录路由规则
        {
            path: '/login',
            component: Login
        },

        //注册界面路由
        {
            path: '/register',
            component: Register
        },
        //主界面
        {
            path: '/game',
            component: Game
        },
        //空白界面用于刷新
        {
            path: '/nothing',
            component: Nothing
        }
    ]
})

//守卫
router.beforeEach(function (to, f, next) {
    if (pathArr.indexOf(to.path) !== -1) {
        const token = localStorage.getItem('token')
        if (token) {
            next()
        } else {
            next('/login')
        }
    } else {
        next()
    }
})
//4.向外共享路由的实例对象
export default router